﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace JU.Common
{
    public class SendMail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="msg">邮件内容</param>
        //public static void SendMailLocalhost(string SmtpServer,int SmtpPort)
        //{
        //    try
        //    {
        //        //构造一个Email的Message对象
        //        MailMessage message = new MailMessage();

        //        //确定smtp服务器地址。实例化一个Smtp客户端
        //        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(Global.SmtpServer);//();
        //        if (Global.SmtpPort != null)
        //            client.Port = (int)Global.SmtpPort;

        //        //构造发件人地址对象
        //        message.From = new MailAddress(Global.SendEmailFrom, Global.SendEmailFromName, Encoding.UTF8);

        //        //构造收件人地址对象
        //        for (int i = 0; i < Global.SendEmailTo.Count; i++)
        //        {
        //            message.To.Add(new MailAddress(Global.SendEmailTo[i], Global.SendEmailToName[i], Encoding.UTF8));
        //        }

        //        #region 添加附件
        //        //Attachment attach = new Attachment(fileName);
        //        ////得到文件的信息
        //        //ContentDisposition disposition = attach.ContentDisposition;
        //        //disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
        //        //disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
        //        //disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
        //        ////向邮件添加附件
        //        //message.Attachments.Add(attach); 
        //        #endregion

        //        //添加邮件主题和内容
        //        message.Subject = Global.EmailTitle;
        //        message.SubjectEncoding = Encoding.UTF8;
        //        if (Global.Messages.Count == 0)
        //            Global.Messages.Add(new Message(null, null, msg, ErrorLevel.Error));
        //        message.Body = ReplaceText();
        //        message.BodyEncoding = Encoding.UTF8;

        //        //设置邮件的信息
        //        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        //        message.BodyEncoding = System.Text.Encoding.UTF8;

        //        //设置为HTML格式邮件
        //        message.IsBodyHtml = true;

        //        //如果服务器支持安全连接，则将安全连接设为true。
        //        //gmail,qq支持，163不支持
        //        client.EnableSsl = Global.EnableSsl.ToLower() == "true";

        //        //设置用户名和密码。
        //        //string userState = message.Subject;
        //        client.UseDefaultCredentials = false;
        //        string username = Global.SendEmailFrom;
        //        string passwd = Global.SendEmailFromPwd;
        //        //用户登陆信息
        //        NetworkCredential myCredentials = new NetworkCredential(username, passwd);
        //        client.Credentials = myCredentials;
        //        //发送邮件
        //        client.Send(message);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw (ex);
        //    }
        //}

        //public static string ReplaceText()
        //{
        //    string path = string.Empty;

        //    path = Global.EmailTemplatePath;

        //    if (path == string.Empty)
        //    {
        //        return string.Empty;
        //    }
        //    System.IO.StreamReader sr = new System.IO.StreamReader(path);

        //    StringBuilder sb = new StringBuilder();
        //    string tempStr = string.Empty;
        //    string str = string.Empty;
        //    str = sr.ReadToEnd();

        //    foreach (Message msg in Global.Messages)
        //    {
        //        tempStr = str.Replace("$File$", msg.File == null ? "NA" : msg.File);
        //        tempStr = tempStr.Replace("$Level$", msg.Level.ToString());
        //        tempStr = tempStr.Replace("$Row$", msg.Row==null?"NA":msg.Row.ToString());
        //        tempStr = tempStr.Replace("$TicketCode$", msg.TicketCode==null?"NA":msg.TicketCode);
        //        tempStr = tempStr.Replace("$Reason$", msg.Reason);
        //        sb.Append(tempStr);
        //    }

        //    return sb.ToString();
        //}
    }
}
